package com.foodie.servlet;

import com.foodie.entity.User;
import com.foodie.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    UserService userServlet = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String acc = req.getParameter("account");
        if (userServlet.checkUser(acc)) {
            req.getSession().setAttribute("msg", "账号已存在");
            resp.sendRedirect(req.getContextPath() + "/register.jsp");
            return;
        }
//        if(acc == "" || acc == null){
//            req.getSession().setAttribute("msg", "账号不能为空");
//            resp.sendRedirect(req.getContextPath() + "/register.jsp");
//            return;
//        }

        String pwd = req.getParameter("password");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        User user = new User();
        user.setName(name);
        user.setUser_id(acc);
        user.setUser_pwd(pwd);
        user.setSex(sex);
        if (userServlet.register(user) != null) {
            req.getSession().setAttribute("msg", "注册成功");
            resp.sendRedirect(req.getContextPath() + "/login.jsp");
        } else {
            req.getSession().setAttribute("msg", "注册失败");
            resp.sendRedirect(req.getContextPath() + "/register.jsp");
        }

    }
}
